{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import snntorch as snn\n",
    "import torch\n",
    "import nir\n",
    "from snntorch.export_nir import export_to_nir\n",
    "from snntorch.import_nir import import_from_nir"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generating an example NIR Graph from snntorch model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2D sample data \n",
    "sample_data = torch.randn(1, 1, 28, 28)  #input image size is 28x28 and 1 channel #the dimension here is batch_size*number_of_channels*height*width\n",
    "\n",
    "class NetWithAvgPool(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(NetWithAvgPool, self).__init__()\n",
    "        self.conv1 = torch.nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1)\n",
    "        self.lif1 = snn.Leaky(beta=0.9, init_hidden=True)\n",
    "        self.fc1 = torch.nn.Linear(28*28*16 // 4, 500)  # Adjusting input size after pooling layer\n",
    "        self.lif2 = snn.Leaky(beta=0.9, init_hidden=True, output=True)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = torch.nn.functional.avg_pool2d(self.conv1(x), kernel_size=2, stride=2)  #  kernel_size=2 and stride=2 for avg_pool2d\n",
    "        x = x.view(-1, 28*28*16 // 4)  # Adjusting the view based on the output shape after pooling\n",
    "        x = self.lif1(x)\n",
    "        x = self.fc1(x)\n",
    "        x = self.lif2(x)\n",
    "        return x\n",
    "\n",
    "net_with_avgpool = NetWithAvgPool()\n",
    "nir_graphtest = export_to_nir(net_with_avgpool, sample_data, ignore_dims=[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nir_graphtest.infer_types()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nir.write(f\"./snnTorch/examples/testconv2d+avgpool.nir\", nir_graphtest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "nir_graph = nir.read(\"testconv2d+avgpool.nir\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#dict_keys(['conv1', 'fc1', 'input', 'lif1', 'lif2', 'output'])\n",
    "nir_graph.nodes['lif1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LIF(tau=0.0009999997615814777, r=9.999997615814776, v_leak=0.0, v_threshold=1.0, input_type={'input': array([], dtype=float64)}, output_type={'output': array([], dtype=float64)}, metadata={})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nir_graph.nodes['lif1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nir_graph.nodes.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nir_graph.edges"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
